package bb.lanxing.net.retrofit

import bb.lanxing.App
import bb.lanxing.net.ApiClientConfigs.DEFAULT_API_CONFIG
import bb.lanxing.net.XossDefaultApiClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class XossRetrofitApiClient private constructor() : XossDefaultApiClient(
    DEFAULT_API_CONFIG
) {
    fun <T> create(cls: Class<T>): T {
        val t: T = retrofit.create(cls)
        return t
    }

    init {
        retrofit = Retrofit.Builder().client(apiClient.okHttpClient)
            .addConverterFactory(GsonConverterFactory.create()).baseUrl(
                config.baseUrl()
            ).build()
    }

    companion object {
        val userAgent = Pair<String, String>("User-Agent", App.getContext().userAgent)
        private lateinit var retrofit: Retrofit

        @Volatile
        private var instance: XossRetrofitApiClient? = null
        fun getInstance() = instance ?: synchronized(this) { instance ?: XossRetrofitApiClient() }
    }
}
